package com.laozhao.msas.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;


public class HelpDialog {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HelpDialog window = new HelpDialog();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public HelpDialog() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout());
		
		JButton btnRunBack = new JButton("Run Back");
		btnRunBack.setBounds(10, 32, 89, 23);
		btnRunBack.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				test2();
			}
		});
		frame.getContentPane().add(btnRunBack,BorderLayout.SOUTH);
		
		frame.getContentPane().add(progressBar,BorderLayout.NORTH);
		frame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
	}
	
	class PrimeNumbersTask extends SwingWorker<List<Integer>, Integer> {
		boolean enough = false;
		List<Integer> numbers = new ArrayList<Integer>(100);
		
		@Override
		public List<Integer> doInBackground() {
			Integer number = 0;
			while (! enough && ! isCancelled()) {
				number = nextPrimeNumber(number);
				publish(number);
				setProgress(number);
				numbers.add(number);
				enough = (number==100);
		     }
			return numbers;
		}

		@Override
		protected void process(List<Integer> chunks) {
			for (Integer number :chunks) {
				textArea.append(number + "\n");
				System.out.println("area is"+number);
		    }
		}
		@Override  
        protected void done() {  
        	try {
				textArea.append("处理时间为" + get().toString()+ "\n");
				System.out.println("area is"+get().toString());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
        }  

		private Integer nextPrimeNumber(int i) {
			try {
				System.out.println("num is:"+i);
				Thread.sleep(100L);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return i+1;
		}
	};

	JTextArea textArea = new JTextArea();
	final JProgressBar progressBar = new JProgressBar(0, 100);

	public void test2() {
		PrimeNumbersTask task = new PrimeNumbersTask();
		task.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if ("progress".equals(evt.getPropertyName())) {
					progressBar.setValue((Integer) evt.getNewValue());
				}
			}
		});
		task.execute();
	}
}
